﻿using System;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;

namespace BillUpload.Util
{
    class TokenUtil
    {

        public static string GetToken(String jsonStr)
        {
            //加密部分
            String mac = GetNetCardMACAddress();
            String dateStr = DateTime.Now.ToString("yyyyMMddHHmm");
            String d1 = dateStr.Substring(0, 6);
            String d2 = dateStr.Substring(6, 6);
            String secretStr = RadixConversion(d2) + RadixConversion(mac) + RadixConversion(d1);
            //文件名部分
  
            String secretFile = RadixConversion(GetMD5String(dateStr + jsonStr + mac));

            //Console.WriteLine(dateStr + " - " + fileName + " - " + mac + "\n" + GetMD5String(dateStr + fileName + mac));

            String mergeStr = secretStr + secretFile;
            String resultStr = "";
            for (int i = 0; i < mergeStr.Length; i += 8)
            {
                resultStr += mergeStr.Substring(i, 8) + "_";
            }
            return resultStr.Substring(0, 125);
        }


        // 获取网卡地址
        private static string GetNetCardMACAddress()
        {
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                // 过滤逻辑（已修正IsReceiveOnly问题）
                if (nic.OperationalStatus == OperationalStatus.Up &&          // 接口已启用
                    nic.NetworkInterfaceType != NetworkInterfaceType.Loopback &&  // 排除回环
                    nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel &&    // 排除隧道
                    !string.IsNullOrEmpty(nic.GetPhysicalAddress().ToString()))   // 确保有物理地址
                {
                    return nic.GetPhysicalAddress().ToString(); // 返回第一个符合条件的MAC地址
                }
            }
            throw new Exception("程序运行失败.");            
        }

        //进制转换
        private static string RadixConversion(String str)
        {
            string resultNum = "";
            str = str.ToUpper();
            foreach (char s in str)
            {
                if (char.IsDigit(s))
                {
                    resultNum += "0" + s;
                }
                else
                {
                    switch (s.ToString())
                    {
                        case "A":
                            resultNum += "16";
                            break;
                        case "B":
                            resultNum += "25";
                            break;
                        case "C":
                            resultNum += "34";
                            break;
                        case "D":
                            resultNum += "43";
                            break;
                        case "E":
                            resultNum += "52";
                            break;
                        case "F":
                            resultNum += "61";
                            break;
                    }
                }
            }
            return resultNum;
        }

        //通过字符串获取MD5值，返回32位字符串。
        public static string GetMD5String(string text)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(text));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString().ToLower();
        }              
    }
}
